package com.example.software.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.example.software.entity.MedicineEntity;
import com.example.software.entity.Result;
import com.example.software.service.MedicineService;
import com.example.software.utils.PageUtils;
import com.example.software.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.example.software.entity.MedicinetypeEntity;
import com.example.software.service.MedicinetypeService;




/**
 * 
 *
 * @author wxy
 * @email wxy9496099@163.com
 * @date 2024-10-31 21:55:40
 */
@RestController
@Slf4j
@Api(tags = "药品类别相关接口")
@RequestMapping("software/medicinetype")
public class MedicinetypeController {
    @Autowired
    private MedicinetypeService medicinetypeService;

    @Autowired
    private MedicineService medicineService;

    /**
     * 列表
     */
    @GetMapping ("/list")
    @ApiOperation(value = "药品类别列表查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = true),
            @ApiImplicitParam(name = "size", value = "每页数量",dataType = "int"),
            @ApiImplicitParam(name = "state", value = "状态，0为下架，1为上架，不传为全部"),
    })
    // @RequiresPermissions("software:medicine:list")
    public Result<PageUtils> list(@RequestParam String page,
                                  @RequestParam String size,
                                  @RequestParam(required = false)  Integer state){
        Map<String, Object> params = new HashMap<>();
        params.put("page",page);
        params.put("limit",size);

        if (state != null) {
            params.put("state", state);
        }

        PageUtils page2 = medicinetypeService.queryPage(params);

        return Result.success(page2);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "根据id查询药品类别信息")
    // @RequiresPermissions("software:medicinetype:info")
    public Result<MedicinetypeEntity> info(@PathVariable("id") Long id){
		MedicinetypeEntity medicinetype = medicinetypeService.getById(id);

        return Result.success(medicinetype);
    }

    /**
     * 保存
     */
    @PostMapping
    @ApiOperation(value = "新增药品类别")
    // @RequiresPermissions("software:medicinetype:save")
    public R save(@RequestBody MedicinetypeEntity medicinetype){
		medicinetypeService.save(medicinetype);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation(value = "修改药品类别")
    // @RequiresPermissions("software:medicinetype:update")
    public R update(@RequestBody MedicinetypeEntity medicinetype){

        if (medicinetype.getState() != null){

                List<Long> medicineIds = medicineService.getMedicineIdsByTypeId(medicinetype.getId());
                if (medicineIds != null && !medicineIds.isEmpty()){
                    for (Long medicineId : medicineIds) {
                        MedicineEntity medicine = new MedicineEntity();
                        medicine.setId(medicineId);
                        medicine.setState(medicinetype.getState());
                        medicineService.updateById(medicine);
                    }
                }

        }
		medicinetypeService.updateById(medicinetype);

        return R.ok();
    }


    //下架药品类型

    /**
     * 删除
     */
    @DeleteMapping
    @ApiOperation(value = "根据id批量删除药品类别信息")
    // @RequiresPermissions("software:medicinetype:delete")
    public R delete(@RequestBody Long[] ids){
		medicinetypeService.removeByIds(Arrays.asList(ids));

        for (Long id : ids) {
            List<Long> medicineIds = medicineService.getMedicineIdsByTypeId(id);
            if (medicineIds != null && !medicineIds.isEmpty()){
                for (Long medicineId : medicineIds) {
                    medicineService.removeById(medicineId);
                }
            }
        }

        return R.ok();
    }

}
